package com.btr.ivle;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.btr.ivle.R;
import com.btr.ivle.backgroud.AsyncTaskCompleteListener;
import com.btr.ivle.backgroud.PostForumTask;
import com.btr.ivle.helper.ForumPostListAdapter;

public class PostForumActivity extends SherlockActivity implements 
	AsyncTaskCompleteListener<String>{
	
	PostForumTask postForumTask;
	EditText TitleET;
	EditText ReplyET;
	String ID;
	Boolean newPost, posted=false;
	ProgressDialog progressDialog;
	final Context context = this;
	public final static String MSG_PSOTED = ".PostForumActivity.Msg.Posted";
	
	public void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.Theme_Sherlock_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forumpost);
		
		String Title,Reply;
		TitleET = (EditText) findViewById(R.id.Title);
        ReplyET = (EditText) findViewById(R.id.Reply);
		Intent intent = getIntent();
		newPost = intent.getBooleanExtra("newPost", false);
		if(newPost){
			ID = intent.getStringExtra(ListForumPostActivity.MSG_HeadingID);
		}else{
		    ID = intent.getStringExtra(ForumPostListAdapter.MSG_ID);
		    Title = intent.getStringExtra(ForumPostListAdapter.MSG_TITLE);
		    Reply = intent.getStringExtra(ForumPostListAdapter.MSG_REPLY);
	        TitleET.setText(Title);
	        ReplyET.setText(Reply);
		}
	    
	    ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowHomeEnabled(false);
        
        //postForumTask.execute(new String[]{ID, Title, Reply});

	}
	
	@SuppressWarnings("deprecation")
	public void post (View view){
		Button b = (Button) view;
		Button d = (Button) findViewById(R.id.ButtonDiscard);
		
		String htmltext;
		htmltext = ReplyET.getText().toString().replace("\n", "<br />");
		
		if(!TitleET.getText().toString().trim().equals("")&&!htmltext.trim().equals("")){
			b.setEnabled(false);
			d.setEnabled(false);
			progressDialog = ProgressDialog.show(this,"Posting...","Please wait...",true);
			postForumTask = new PostForumTask(this);
			postForumTask.execute(new String[]{ID, TitleET.getText().toString(), htmltext, String.valueOf(newPost)});
		}else{
			AlertDialog ad = new AlertDialog.Builder(context).create();
			ad.setMessage("Title or body is missing");
			ad.setButton("Retry", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which){
					return;
				}
			});
			ad.show();
		}
			
	}
	
	public void discard (View view){
		super.onBackPressed();
	}
	
	@Override
    public boolean onOptionsItemSelected(final MenuItem item) 
    {
    	switch (item.getItemId()) {
        case android.R.id.home:
          super.onBackPressed();
    	}
          return true;
    }
	
	public void onAsyncTaskComplete(String result) {
		try {
			if(postForumTask.get().length()==38){
				posted = true;
			}
			else{
				posted = false;
			}
				
		} catch (InterruptedException e) {
			e.printStackTrace();
			posted = false;
		} catch (ExecutionException e) {
			e.printStackTrace();
			posted = false;
		}
		progressDialog.dismiss();
		if (posted){
			Toast.makeText(this, "Successfully Posted." , Toast.LENGTH_LONG).show();
			Intent intent = new Intent();
	    	intent.putExtra(MSG_PSOTED, posted);
	    	setResult(Activity.RESULT_OK, intent);
	    	finish();
			super.onBackPressed();
		}else{
			Toast.makeText(this, "Failed to Post. Please try again later" , Toast.LENGTH_LONG).show();
			Button b = (Button) findViewById(R.id.ButtonSubmit);
			Button d = (Button) findViewById(R.id.ButtonDiscard);
			b.setEnabled(true);
			d.setEnabled(true);
		}
	}
	
	public void onDestroy(){
    	if(this.postForumTask!=null)
    		this.postForumTask.cancel(true);
    	if (progressDialog!=null)
    		progressDialog.dismiss();
    	super.onDestroy();
    }
	
//    protected void onRestart(){
//    	Log.i("PostForumActivity","onRestart");
//    	super.onRestart();
//    }
//
//    protected void onResume(){
//    	Log.i("PostForumActivity","onResume");
//    	super.onResume();
//    }
//
//    protected void onPause(){
//    	Log.i("PostForumActivity","onPause");
//    	super.onPause();
//    }
//
//    protected void onStop(){
//    	Log.i("PostForumActivity","onStop");
//    	super.onDestroy();
//    }

}